﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using XYAC.Framework.MonitorTireMaterialOnline.Extensions;

namespace XYAC.Framework.MonitorTireMaterialOnline.Context
{
    public partial class CurrentHisDataContext : HisDataContext
    {
        private DateOnly dateOnly;

        public CurrentHisDataContext(IOptionsMonitor<AppConfig> appConfig)
        {
            AppConfig = appConfig;
            if (AppConfig.CurrentValue.IsUseCustomizePlanDate == true && AppConfig.CurrentValue?.CustomizePlanDate.HasValue == true)
            {
                var tempDate = AppConfig.CurrentValue?.CustomizePlanDate.Value.Date;
                Date = DateOnly.FromDateTime(tempDate.Value);
            }
            else
            {
                Date = DateOnly.FromDateTime(DateTime.Now);
            }
        }

        public IOptionsMonitor<AppConfig> AppConfig { get; }
        public DateOnly Date { get; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            // 配置 SQL Server 数据库连接字符串
            //optionsBuilder.UseJet(AppConfig?.CurrentValue?.AccessCAreaHisConnectionString);
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseJet(AppConfig?.CurrentValue?.AccessCAreaHisConnectionString.Replace("{DateNow}", Date.ToString("yyyy-MM-dd")));
            }

        }

    }
}
